package org.wesc.boot.dao.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Wesley
 * @date 2019/7/23
 */
public class CodeGenerator {

    private static final String BASE_MODULE = "boot-core-dao";

    /**
     * 基本包名
     */
    private static final String BASE_PACKAGE = "org.wesc.boot";

    /**
     * 作者
     */
    private static final String AUTHOR = "Wesley";

    /**
     * 要生成的表名前缀和表名
     */
    private static final String TABLE_PREFIX = "";

    private static final String[] TABLE_LIST = {
            "sys_log"
    };

    /**
     * 数据库配置四要素
     */
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/wesc_boot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "root";


    public static void main(String[] args) {

        AutoGenerator gen = new AutoGenerator();

        // 数据库配置
        gen.setDataSource(new DataSourceConfig()
                .setDbType(DbType.MYSQL)
                .setDriverName(DRIVER_NAME)
                .setUrl(URL)
                .setUsername(USER_NAME)
                .setPassword(PASSWORD)
        );

        // 全局配置
        gen.setGlobalConfig(new GlobalConfig()
                .setOutputDir(System.getProperty("user.dir") + "/" + BASE_MODULE + "/src/main/java")
                .setFileOverride(true)
                .setActiveRecord(true)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setOpen(false)
                .setAuthor(AUTHOR)
                // 自定义文件命名，注意 %s 会自动填充表实体属性！
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
        );

        // 策略配置
        gen.setStrategy(new StrategyConfig()
                // 全局大写命名
                .setCapitalMode(true)
                // 表名生成策略
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                // 需要生成的表名前缀
                .setTablePrefix(TABLE_PREFIX)
                .setInclude(TABLE_LIST)
                .setEntityLombokModel(true)
                .setEntityColumnConstant(true)
        );

        // 包配置
        gen.setPackageInfo(new PackageConfig()
                .setParent(BASE_PACKAGE)
                .setModuleName("dao")
                .setEntity("entity")
                .setMapper("mapper")
                .setService("service")
                .setServiceImpl("service.impl")
                .setXml("mapper")
        );

        // 注入自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do sth
            }
        };
        List<FileOutConfig> fileOutList = new ArrayList<>();
        /*mapper*/
        fileOutList.add(new FileOutConfig("templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return System.getProperty("user.dir") + "/" + BASE_MODULE + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(fileOutList);
        gen.setCfg(cfg);

        // 指定模板引擎 默认是VelocityTemplateEngine ，需要引入相关引擎依赖
        gen.setTemplateEngine(new FreemarkerTemplateEngine());

        // 模板配置
        gen.setTemplate(new TemplateConfig()
                .setXml(null)
                .setController(null)
                .setEntity("templates/entity.java")
                .setMapper("templates/mapper.java")
                .setService("templates/service.java")
                .setServiceImpl("templates/serviceImpl.java")
        );

        // 执行生成
        gen.execute();
    }
}
